package com.onpositive.realmview.queryeditor;

import java.util.Collection;
import java.util.Iterator;
import java.util.Set;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.ui.IElementFactory;
import org.eclipse.ui.IMemento;

import com.onpositive.realmview.AbstractRealmView;
import com.onpositive.realmview.IRealmView;
import com.onpositive.realmview.model.IHasViews;
import com.onpositive.realmview.model.IModelObject;
import com.onpositive.realmview.model.realm.IRealmPosition;
import com.onpositive.realmview.queryeditor.ClassificationRealmPosition.Classification;

public abstract class AbstractURLInputFactory implements IElementFactory
{
	
    /**
     * Factory id. Plugin registers factory with this name
     */
    protected static final String ID_FACTORY = "com.onpositive.wiki.ui.persistance.QueryInputFactory"; //$NON-NLS-1$
    
    /**
     * Tag for the artifact url
     */
    protected static final String TAG_URL = "parent_url"; //$NON-NLS-1$

	public IAdaptable createElement(IMemento memento)
	{
		 // Get the file name.
        String url = memento.getString(TAG_URL);
        if (url == null) {
			return null;
		}
        String viewName = null;
        int lastIdx = url.lastIndexOf('$');
        if (lastIdx > -1)
        {
        	viewName = url.substring(lastIdx + 1);
        	url = url.substring(0, lastIdx);
        }
        return createFromUrl(url, viewName, memento);
	}
	
	protected abstract IAdaptable createFromUrl(String url, String viewName, IMemento memento);

	protected IRealmView trySearchPropertyRealm(String viewName, Collection<IRealmPosition> positions, String key, String value)
	{
		for (Iterator<IRealmPosition> iterator = positions.iterator(); iterator.hasNext();)
		{
			IRealmPosition position = (IRealmPosition) iterator.next();
			if (position instanceof ClassificationRealmPosition)
			{
				Classification[] classifications = ((ClassificationRealmPosition) position).getClassifications();
				for (int i = 0; i < classifications.length; i++)
				{
					if (classifications[i].property.equalsIgnoreCase(key) && classifications[i].value.equals(value))
					{
						Set<IModelObject> relatedObjects = position.relatedObjects();
						for (Iterator<IModelObject> iterator2 = relatedObjects.iterator(); iterator2.hasNext();)
						{
							IModelObject object = (IModelObject) iterator2.next();
							if (object instanceof AbstractRealmView && ((AbstractRealmView) object).getName().equals(viewName))
								return (IRealmView) object;
						}
					}
				}
				IRealmView result = trySearchPropertyRealm(viewName, position.positions().getContents(), key, value);
				if (result != null)
					return result;
			}			
		}
		return null;
	}
	
    /**
     * Returns the element factory id for this class.
     * 
     * @return the element factory id
     */
    public static String getFactoryId() {
        return ID_FACTORY;
    }
    
    /**
     * Saves the state of the given file editor input into the given memento.
     *
     * @param memento the storage area for element state
     * @param view the {@link PageInput} to save
     */
    public static void saveState(IMemento memento, IRealmView view) {
        IHasViews<?> parent = view.getParent();
        if (parent == null)
        {
        	return;
        }
		String url = parent.getUrl();
		url += "$" + view.getName();
        if (url != null)
        {
        	memento.putString(TAG_URL, url);
        }
    }

}
